import request from '../../utils/request'

Page({
  data: {
    backendUrl: 'http://127.0.0.1:88/api/user/',
    coach: null,
    certList: [],
    achieveList: []
  },

  onLoad(options) {
    const coachId = options.id;
    this.loadCoachDetail(coachId);
  },

  // 加载教练详情
  loadCoachDetail(id) {
    wx.request({
      url: this.data.backendUrl + 'get_coach_detail.php',
      data: { id: id },
      success: (res) => {
        if (res.data.status === 'success') {
          const coach = res.data.coach;
          // 将证书和成就字符串转换为数组
          const certList = coach.certification ? coach.certification.split('\n') : [];
          const achieveList = coach.achievement ? coach.achievement.split('\n') : [];
          
          this.setData({
            coach: coach,
            certList: certList,
            achieveList: achieveList
          });
        }
      }
    });
  },

  // 预约按钮点击
  handleBook() {
    // 检查是否登录
    const userInfo = wx.getStorageSync('userInfo');
    if (!userInfo) {
      wx.showToast({
        title: '请先登录',
        icon: 'none',
        duration: 1500
      });
      setTimeout(() => {
        wx.navigateTo({
          url: '/pages/login/login'
        });
      }, 1500);
      return;
    }
    // 跳转到新的预约页面
    wx.navigateTo({
      url: `/pages/booking-coach/booking-coach?id=${this.data.coach.id}`,
      icon: 'none',
      duration: 1500
    });
  },

  // 新增查看评价点击处理
  handleViewEval() {
    wx.navigateTo({
      url: `/pages/evaluate/evaluate?id=${this.data.coach.id}`,
      icon: 'none',
      duration: 1500
    });
  }

}); 